home *** CD-ROM | disk | FTP | other *** search
/ IRIX Patches 1995 June / SGI IRIX Patches 1995 Jun.iso / 5.3_patches / patchSG0000154 / patchSG0000154.idb / usr / share / src / OpenGL / samples / copy.c.z / copy.c
Encoding:
C/C++ Source or Header  |  1995-06-12  |  3.1 KB  |  174 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include "tk.h"
  9.  
  10.  
  11. GLenum doubleBuffer, directRender;
  12. GLint windW, windH;
  13.  
  14. char *fileName = 0;
  15. TK_RGBImageRec *image;
  16. float point[3];
  17. float zoom;
  18. GLint x, y;
  19.  
  20.  
  21. static void Init(void)
  22. {
  23.  
  24.     glClearColor(0.0, 0.0, 0.0, 0.0);
  25.  
  26.     x = 0;
  27.     y = windH;
  28.     zoom = 1.8;
  29. }
  30.  
  31. static void Reshape(int width, int height)
  32. {
  33.  
  34.     windW = (GLint)width;
  35.     windH = (GLint)height;
  36.  
  37.     glViewport(0, 0, windW, windH);
  38.  
  39.     glMatrixMode(GL_PROJECTION);
  40.     glLoadIdentity();
  41.     gluOrtho2D(0, windW, 0, windH);
  42.     glMatrixMode(GL_MODELVIEW);
  43. }
  44.  
  45. static GLenum Key(int key, GLenum mask)
  46. {
  47.  
  48.     switch (key) {
  49.       case TK_ESCAPE:
  50.         tkQuit();
  51.       case TK_Z:
  52.     zoom += 0.2;
  53.     break;
  54.       case TK_z:
  55.     zoom -= 0.2;
  56.     if (zoom < 0.2) {
  57.         zoom = 0.2;
  58.     }
  59.     break;
  60.       default:
  61.     return GL_FALSE;
  62.     }
  63.     return GL_TRUE;
  64. }
  65.  
  66. static GLenum Mouse(int mouseX, int mouseY, GLenum button)
  67. {
  68.  
  69.     x = (GLint)mouseX;
  70.     y = (GLint)mouseY;
  71.     return GL_TRUE;
  72. }
  73.  
  74. static void Draw(void)
  75. {
  76.  
  77.     glClear(GL_COLOR_BUFFER_BIT);
  78.  
  79.     point[0] = (windW / 2) - (image->sizeX / 2);
  80.     point[1] = (windH / 2) - (image->sizeY / 2);
  81.     point[2] = 0;
  82.     glRasterPos3fv(point);
  83.  
  84.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  85.     glPixelZoom(1.0, 1.0);
  86.     glDrawPixels(image->sizeX, image->sizeY, GL_RGB, GL_UNSIGNED_BYTE,
  87.          image->data);
  88.  
  89.     point[0] = (float)x;
  90.     point[1] = windH - (float)y;
  91.     point[2] = 0.0;
  92.     glRasterPos3fv(point);
  93.  
  94.     glPixelZoom(zoom, zoom);
  95.     glCopyPixels((windW/2)-(image->sizeX/2),
  96.          (windH/2)-(image->sizeY/2),
  97.          image->sizeX, image->sizeY, GL_COLOR);
  98.  
  99.     glFlush();
  100.  
  101.     if (doubleBuffer) {
  102.     tkSwapBuffers();
  103.     }
  104. }
  105.  
  106. static GLenum Args(int argc, char **argv)
  107. {
  108.     GLint i;
  109.  
  110.     doubleBuffer = GL_FALSE;
  111.     directRender = GL_TRUE;
  112.  
  113.     for (i = 1; i < argc; i++) {
  114.     if (strcmp(argv[i], "-sb") == 0) {
  115.         doubleBuffer = GL_FALSE;
  116.     } else if (strcmp(argv[i], "-db") == 0) {
  117.         doubleBuffer = GL_TRUE;
  118.     } else if (strcmp(argv[i], "-dr") == 0) {
  119.         directRender = GL_TRUE;
  120.     } else if (strcmp(argv[i], "-ir") == 0) {
  121.         directRender = GL_FALSE;
  122.     } else if (strcmp(argv[i], "-f") == 0) {
  123.         if (i+1 >= argc || argv[i+1][0] == '-') {
  124.         printf("-f (No file name).\n");
  125.         return GL_FALSE;
  126.         } else {
  127.         fileName = argv[++i];
  128.         }
  129.     } else {
  130.         printf("%s (Bad option).\n", argv[i]);
  131.         return GL_FALSE;
  132.     }
  133.     }
  134.     return GL_TRUE;
  135. }
  136.  
  137. void main(int argc, char **argv)
  138. {
  139.     GLenum type;
  140.  
  141.     if (Args(argc, argv) == GL_FALSE) {
  142.     tkQuit();
  143.     }
  144.  
  145.     if (fileName == 0) {
  146.     printf("No image file.\n");
  147.     tkQuit();
  148.     }
  149.  
  150.     image = tkRGBImageLoad(fileName);
  151.  
  152.     windW = 300;
  153.     windH = 300;
  154.     tkInitPosition(0, 0, windW, windH);
  155.  
  156.     type = TK_RGB;
  157.     type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  158.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  159.     tkInitDisplayMode(type);
  160.  
  161.     if (tkInitWindow("Copy Test") == GL_FALSE) {
  162.     tkQuit();
  163.     }
  164.  
  165.     Init();
  166.  
  167.     tkExposeFunc(Reshape);
  168.     tkReshapeFunc(Reshape);
  169.     tkKeyDownFunc(Key);
  170.     tkMouseDownFunc(Mouse);
  171.     tkDisplayFunc(Draw);
  172.     tkExec();
  173. }
  174.